<template>
  <div class="product-detail">
    <div class="page-header">
      <h2>产品详情</h2>
      <p>查看产品的详细信息</p>
    </div>
    
    <div class="content-section" v-if="product">
      <div class="detail-grid">
        <!-- 产品图片 -->
        <div class="image-section">
          <div class="main-image">
            <img :src="product.main_image_url || '/api/placeholder/400/400'" 
                 :alt="product.name" 
                 @error="handleImageError">
          </div>
        </div>
        
        <!-- 基本信息 -->
        <div class="info-section">
          <div class="info-card">
            <h3>基本信息</h3>
            <div class="info-grid">
              <div class="info-item">
                <label>产品名称</label>
                <span>{{ product.name }}</span>
              </div>
              <div class="info-item">
                <label>产品ID</label>
                <span>{{ product.id }}</span>
              </div>
              <div class="info-item">
                <label>品牌</label>
                <span>{{ getBrandName(product.brand_id) }}</span>
              </div>
              <div class="info-item">
                <label>分类</label>
                <span>{{ getTypeName(product.type_id) }}</span>
              </div>
              <div class="info-item">
                <label>颜色</label>
                <span>{{ getColorName(product.color_id) }}</span>
              </div>
              <div class="info-item">
                <label>尺寸</label>
                <span>{{ getSizeName(product.size_id) }}</span>
              </div>
            </div>
          </div>
          
          <!-- 价格库存信息 -->
          <div class="info-card">
            <h3>价格库存</h3>
            <div class="info-grid">
              <div class="info-item">
                <label>原价</label>
                <span class="price">¥{{ product.origin_price }}</span>
              </div>
              <div class="info-item">
                <label>折扣价</label>
                <span class="price discount">{{ product.discount_price ? `¥${product.discount_price}` : '无' }}</span>
              </div>
              <div class="info-item">
                <label>库存数量</label>
                <span :class="{ 'low-stock': product.stock < 10 }">{{ product.stock }} 件</span>
              </div>
              <div class="info-item">
                <label>状态</label>
                <span :class="['status', product.status === 1 ? 'active' : 'inactive']">
                  {{ product.status === 1 ? '上架' : '下架' }}
                </span>
              </div>
            </div>
          </div>
          
          <!-- 其他信息 -->
          <div class="info-card">
            <h3>其他信息</h3>
            <div class="info-grid">
              <div class="info-item">
                <label>产地</label>
                <span>{{ product.origin_place || '未设置' }}</span>
              </div>
              <div class="info-item">
                <label>创建时间</label>
                <span>{{ formatDate(product.created_at) }}</span>
              </div>
              <div class="info-item">
                <label>更新时间</label>
                <span>{{ formatDate(product.updated_at) }}</span>
              </div>
            </div>
          </div>
          
          <!-- 产品描述 -->
          <div class="info-card" v-if="product.description">
            <h3>产品描述</h3>
            <div class="description">
              {{ product.description }}
            </div>
          </div>
        </div>
      </div>
      
      <!-- 操作按钮 -->
      <div class="action-buttons">
        <button type="button" @click="goBack" class="btn btn-secondary">
          <i class="icon">↩️</i>
          返回列表
        </button>
        <button type="button" @click="editProduct" class="btn btn-primary">
          <i class="icon">✏️</i>
          编辑产品
        </button>
        <button type="button" @click="deleteProduct" class="btn btn-danger">
          <i class="icon">🗑️</i>
          删除产品
        </button>
      </div>
    </div>
    
    <!-- 产品不存在的提示 -->
    <div class="content-section" v-else>
      <div class="empty-state">
        <div class="empty-icon">📦</div>
        <h3>产品不存在</h3>
        <p>未找到指定的产品信息</p>
        <button type="button" @click="goBack" class="btn btn-primary">
          <i class="icon">↩️</i>
          返回列表
        </button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ProductDetail',
  props: {
    productData: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      product: null,
      brands: [
        { id: 1, name: 'Nike' },
        { id: 2, name: 'Adidas' },
        { id: 3, name: 'Converse' },
        { id: 4, name: 'Vans' },
        { id: 5, name: 'New Balance' },
        { id: 6, name: 'Puma' }
      ],
      types: [
        { id: 1, name: '运动鞋' },
        { id: 2, name: '跑步鞋' },
        { id: 3, name: '帆布鞋' },
        { id: 4, name: '滑板鞋' },
        { id: 5, name: '复古鞋' },
        { id: 6, name: '休闲鞋' }
      ],
      colors: [
        { id: 1, name: '黑色' },
        { id: 2, name: '白色' },
        { id: 3, name: '红色' },
        { id: 4, name: '蓝色' },
        { id: 5, name: '灰色' },
        { id: 6, name: '多色' }
      ],
      sizes: [
        { id: 1, value: '35' },
        { id: 2, value: '36' },
        { id: 3, value: '37' },
        { id: 4, value: '38' },
        { id: 5, value: '39' },
        { id: 6, value: '40' },
        { id: 7, value: '41' },
        { id: 8, value: '42' },
        { id: 9, value: '43' },
        { id: 10, value: '44' },
        { id: 11, value: '45' }
      ]
    };
  },
  mounted() {
    this.loadProduct();
  },
  watch: {
    productData: {
      handler() {
        this.loadProduct();
      },
      immediate: true
    }
  },
  methods: {
    loadProduct() {
      if (this.productData) {
        this.product = { ...this.productData };
        // 添加模拟的时间戳
        if (!this.product.created_at) {
          this.product.created_at = new Date().toISOString();
        }
        if (!this.product.updated_at) {
          this.product.updated_at = new Date().toISOString();
        }
      }
    },
    
    getBrandName(brandId) {
      const brand = this.brands.find(b => b.id === brandId);
      return brand ? brand.name : '未知品牌';
    },
    
    getTypeName(typeId) {
      const type = this.types.find(t => t.id === typeId);
      return type ? type.name : '未知分类';
    },
    
    getColorName(colorId) {
      const color = this.colors.find(c => c.id === colorId);
      return color ? color.name : '未知颜色';
    },
    
    getSizeName(sizeId) {
      const size = this.sizes.find(s => s.id === sizeId);
      return size ? size.value : '未知尺寸';
    },
    
    formatDate(dateString) {
      if (!dateString) return '未设置';
      const date = new Date(dateString);
      return date.toLocaleString('zh-CN');
    },
    
    handleImageError(event) {
      event.target.src = '/api/placeholder/400/400';
    },
    
    goBack() {
      this.$emit('navigate', '商品列表');
    },
    
    editProduct() {
      this.$emit('navigate', '编辑产品', this.product);
    },
    
    deleteProduct() {
      if (confirm(`确定要删除产品 "${this.product.name}" 吗？\n删除后将无法恢复。`)) {
        // 这里应该调用后端API删除产品
        this.showToast(`产品 "${this.product.name}" 已删除`, 'success');
        
        // 延迟返回列表页
        setTimeout(() => {
          this.goBack();
        }, 1500);
      }
    },
    
    showToast(message, type = 'info') {
      const toast = document.createElement('div');
      toast.className = `toast toast-${type}`;
      toast.textContent = message;
      
      Object.assign(toast.style, {
        position: 'fixed',
        top: '20px',
        right: '20px',
        padding: '12px 20px',
        borderRadius: '6px',
        color: 'white',
        fontSize: '14px',
        fontWeight: '500',
        zIndex: '10000',
        opacity: '0',
        transform: 'translateX(100%)',
        transition: 'all 0.3s ease',
        boxShadow: '0 4px 12px rgba(0, 0, 0, 0.2)'
      });

      const colors = {
        success: '#10b981',
        error: '#ef4444',
        warning: '#f59e0b',
        info: '#3b82f6'
      };
      
      toast.style.background = colors[type] || colors.info;
      document.body.appendChild(toast);
      
      setTimeout(() => {
        toast.style.opacity = '1';
        toast.style.transform = 'translateX(0)';
      }, 100);
      
      setTimeout(() => {
        toast.style.opacity = '0';
        toast.style.transform = 'translateX(100%)';
        setTimeout(() => {
          if (document.body.contains(toast)) {
            document.body.removeChild(toast);
          }
        }, 300);
      }, 3000);
    }
  }
};
</script>

<style scoped>
.product-detail {
  padding: 20px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  min-height: 100vh;
}

.page-header {
  margin-bottom: 30px;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  padding: 20px;
  border-radius: 15px;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.page-header h2 {
  margin: 0 0 8px 0;
  font-size: 24px;
  color: white;
  font-weight: 600;
}

.page-header p {
  margin: 0;
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
}

.content-section {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 15px;
  padding: 30px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
}

.detail-grid {
  display: grid;
  grid-template-columns: 400px 1fr;
  gap: 30px;
  margin-bottom: 30px;
}

.image-section {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.main-image {
  background: white;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.main-image img {
  width: 100%;
  height: 360px;
  object-fit: cover;
  border-radius: 8px;
}

.info-section {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.info-card {
  background: rgba(255, 255, 255, 0.7);
  padding: 25px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.info-card h3 {
  margin: 0 0 20px 0;
  font-size: 18px;
  color: #333;
  font-weight: 600;
  border-bottom: 2px solid #667eea;
  padding-bottom: 8px;
}

.info-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 15px;
}

.info-item {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.info-item label {
  font-size: 12px;
  color: #666;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.info-item span {
  font-size: 14px;
  color: #333;
  font-weight: 500;
}

.price {
  font-size: 16px;
  font-weight: 600;
  color: #e74c3c;
}

.price.discount {
  color: #27ae60;
}

.low-stock {
  color: #e74c3c;
  font-weight: 600;
}

.status {
  padding: 4px 12px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
}

.status.active {
  background: #d4edda;
  color: #155724;
}

.status.inactive {
  background: #f8d7da;
  color: #721c24;
}

.description {
  font-size: 14px;
  line-height: 1.6;
  color: #555;
  background: rgba(255, 255, 255, 0.5);
  padding: 15px;
  border-radius: 8px;
  border-left: 4px solid #667eea;
}

.action-buttons {
  display: flex;
  justify-content: center;
  gap: 15px;
  padding-top: 20px;
  border-top: 1px solid #e1e5e9;
}

.btn {
  display: inline-flex;
  align-items: center;
  padding: 12px 24px;
  border: none;
  border-radius: 25px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  text-decoration: none;
}

.btn-primary {
  background: linear-gradient(45deg, #667eea, #764ba2);
  color: white;
  box-shadow: 0 4px 15px rgba(102, 126, 234, 0.3);
}

.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(102, 126, 234, 0.4);
}

.btn-secondary {
  background: linear-gradient(45deg, #95a5a6, #7f8c8d);
  color: white;
  box-shadow: 0 4px 15px rgba(149, 165, 166, 0.3);
}

.btn-secondary:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(149, 165, 166, 0.4);
}

.btn-danger {
  background: linear-gradient(45deg, #e74c3c, #c0392b);
  color: white;
  box-shadow: 0 4px 15px rgba(231, 76, 60, 0.3);
}

.btn-danger:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 20px rgba(231, 76, 60, 0.4);
}

.btn .icon {
  margin-right: 8px;
  font-size: 16px;
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
}

.empty-icon {
  font-size: 64px;
  margin-bottom: 20px;
}

.empty-state h3 {
  margin: 0 0 10px 0;
  font-size: 24px;
  color: #333;
}

.empty-state p {
  margin: 0 0 30px 0;
  color: #666;
  font-size: 16px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .detail-grid {
    grid-template-columns: 1fr;
    gap: 20px;
  }
  
  .info-grid {
    grid-template-columns: 1fr;
  }
  
  .action-buttons {
    flex-direction: column;
  }
  
  .content-section {
    padding: 20px;
  }
}
</style>